#ifndef NOTEINIT
#define NOTEINIT
#include <stdio.h>
#include <wchar.h>
#include "note.h"
extern Note_by_time *time_head;
extern Tag_list *tag_head;
extern Note_by_time *time_node;
extern int note_number;
void add_note_to_tag(Note *temp,Note_list *note_node){//读入笔记
    while(note_node->next!=NULL)
        note_node = note_node->next;
    note_node->next = (Note_list *)malloc(sizeof(Note_list));
    note_node = note_node->next;
    note_node->note = temp;
    note_node->next=NULL;
    return;
}
void add_tag(wchar_t *tag, Note *temp)//读入标签
{
    Tag_list *tag_node=tag_head;
    while(tag_node->tag_next!=NULL){
        if(!wcscmp(tag,tag_node->tag)){
            add_note_to_tag(temp, tag_node->note_list);
            return;
        }
        tag_node = tag_node->tag_next;
    }
    tag_node->tag_next = (Tag_list *)malloc(sizeof(Tag_list));
    tag_node = tag_node->tag_next;
    tag_node->tag_next = NULL;
    wcscpy(tag_node->tag, tag);
    tag_node->note_list = (Note_list *)malloc(sizeof(Note_list));
    tag_node->note_list->note = temp;
    tag_node->note_list->next = NULL;
    return;
}
void read_tag(Note *new){//询问
    wchar_t flag[10]=L"Y";
    int k=0;
    memset(new->tag,0,sizeof(new->tag));
    while(flag[0]!='N'){
        if(!k)wprintf(L"已经有 %d/10 条标签。是否添加标签？(Y)es/(N)o\n",new->tag_number);
        else wprintf(L"是否继续添加标签？(Y)es/(N)o\n");
        wscanf(L"%ls",flag);
        if((flag[0]!='Y'&&flag[0]!='y'&&flag[0]!='N'&&flag[0]!='n')||wcslen(flag)>3){
            wprintf(L"输入不合法，请输入 Y 或者 N\n");
            flag[0]='Y';
            continue;
        }
        if(flag[0]=='N'||flag[0]=='n')break;
        wprintf(L"\n输入标签:");
        wchar_t tt=fgetwc(stdin);
        tt=fgetwc(stdin);
        if(tt==L'\n'){
            wprintf(L"未检测到标签\n");
            continue;
        }
        int len=0;
        do{
            new->tag[k][len++]=tt;
            tt=fgetwc(stdin);
        }while(tt!=L'\n');
        add_tag(new->tag[k++],new);
        new->tag_number++;
        if(new->tag_number==10)return;
    }
}
void read_time(Note *temp,FILE *fi){
    int a,b;
    fwscanf(fi,L"%d/%d/%d", &b, &a,&temp->date);
    temp->date += a * 100+ b * 10000;
    fwscanf(fi,L"%d:%d", &a, &temp->time);
    temp->time += a * 100;
    return;
}
int cmp_over(wchar_t a,wchar_t *note,FILE *fi){//判断笔记结束
    wchar_t over[] = OVER;
    int i,k=0;
    for (i = 0; i < 10;i++){
        if(a!=over[i]){
            k = 1;
            break;
        }
        a = fgetwc(fi);
    }
    if(k)
        for (int j = 0; j < i;j++)
            note[wcslen(note)] = over[j];
    return k;
}
void read_note(Note *temp,FILE *fi){
    memset(temp->note, 0, sizeof(temp->note));
    wchar_t ch = fgetc(fi);
    while(cmp_over(ch,temp->note,fi)){
        temp->note[wcslen(temp->note)] = ch;
        ch=fgetwc(fi);
    }
}
Tag_list* init()
{
    FILE *fi = fopen("note.txt", "r");
    if(fi==NULL){
        // wprintf(L"failed open file文件打开失败\n请检查是否存在note.txt文件");
        // system("pause");
        // return NULL;
        FILE *t=fopen("note.txt","w");
        fi=fopen("note.txt","r");
    }
    time_head = (Note_by_time *)malloc(sizeof(Note_by_time));
    tag_head = (Tag_list *)malloc(sizeof(Tag_list));
    tag_head->tag_next=NULL;
    time_head->next=(Note_by_time*)malloc(sizeof(Note_by_time));
    time_node = time_head->next;
    if(!fwscanf(fi,L"%d", &note_number))
        note_number=0;
    for (int i = 1; i <= note_number;i++)
    {
        time_node->note = (Note *)malloc(sizeof(Note));
        Note *temp=time_node->note;
        temp->tag_number = 0;
        fwscanf(fi,L"%ls", temp->title);
        wchar_t a[550];
        fwscanf(fi,L"%ls", a);
        while (a[0] != '$'){
            wcscpy(temp->tag[temp->tag_number++], a);
            add_tag(a, temp);
            fwscanf(fi, L"%s", a);
        }
        read_time(temp,fi);
        read_note(temp,fi);
        time_node->next = (Note_by_time *)malloc(sizeof(Note_by_time));
        time_node = time_node->next;
    }
    time_node->next = NULL;
    return tag_head;
}
#endif